﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace DAO
{
    public class CConfig
    {

        private static string _Patch = "config.ini";

        public static bool CreateNewFile()
        {
            try
            {
                //StreamWriter sw = new StreamWriter(Application.StartupPath + "\\" + _Patch, false);
                StreamWriter sw = new StreamWriter(_Patch, false);
                sw.Write("[Config]");
                sw.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        //doc file config
        private static string LoadData()
        {
            StreamReader reader = new StreamReader(_Patch);
            string _temp = reader.ReadToEnd();
            reader.Close();
            return _temp;
        }

        private static string GetValueInFile(string Name)
        {
            string text = LoadData();//chuoi default path
            foreach (string line in text.Trim().Split('\n',';'))//cat chuoi path
            {
                if (line.Trim() != null)
                {
                    string[] _data = line.Trim().Split('=');
                    if (_data[0].Trim() == Name)//cot ten
                    {
                        return _data[1].Trim();//cot gia tri
                    }
                }
            }
            return "Unknown";
        }
        /// <summary>
        /// lay gia tri default -> goi DTO
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static string GetValueDefault(string Name)
        {
            
            if (Name == DTO.COptionDefault.Name.DataPath)
                return DTO.COptionDefault.DataPath;//dg dan
            else if (Name == DTO.COptionDefault.Name.DefaultDelay)
                return DTO.COptionDefault.DefaultDelay;//do tre
            else if (Name == DTO.COptionDefault.Name.EnOrDisApp)
                return DTO.COptionDefault.EnOrDisApp;
            else if (Name == DTO.COptionDefault.Name.StopRunning)
                return DTO.COptionDefault.StopRunning;//dung thuc thi
            else
                return "Unknown";
        }
        /// <summary>
        /// lay value file or defuilt
        /// </summary>
        /// <param name="Name">ten cua gia tri can lay datapath or defaultName ....</param>
        /// <returns></returns>
        public static string GetValue(string Name)
        {
            string _temp = "Unknown";
            FileInfo _FileInfo = new FileInfo(_Patch);
            if (_FileInfo.Exists)//ton tai
            {
                _temp = GetValueInFile(Name);
            }
            if (_temp == "Unknown")
            {
                _temp = GetValueDefault(Name);
            }
            return _temp;
        }
        /// <summary>
        /// chinh sua va save defaul config
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public static void SetValue(string Name, String Value)
        {
            FileInfo _FileInfo = new FileInfo(_Patch);
            if (!_FileInfo.Exists)//ton tai
            {
                CreateNewFile();
            }

            string text = LoadData();
            string _temp = GetValueInFile(Name);
            if (_temp == "Unknown")
            {
                text += "\n" + Name + " = " + Value + ";";
            }
            else
            {
                text = text.Replace(_temp, Value);
            }

            StreamWriter sw = new StreamWriter(_Patch, false);
            sw.Write(text);
            sw.Flush();
            sw.Close();
        }
    }
}
